package com.saile.util;

import com.saile.myRunTime.MyRunTimeExcption;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.MalformedURLException;


public class LoadPic {

public static String  LoadPics(MultipartFile multipartFile,String filePath)
{
	
	BufferedInputStream in;
	FileOutputStream file;
	
	String rootPath=GetProperties.readValue("picpath")+"/"+filePath;
	System.out.println("rootPath===>>"+rootPath);
	if (checkfile(rootPath))
	{
		System.out.println("有图不用下了！");
		return rootPath;
	}
	try {
		save(multipartFile.getInputStream(),rootPath);
	} catch (IOException e) {
		e.printStackTrace();
	}
	return filePath;
}
public static String  LoadPics(InputStream is, String filePath)
{

	String rootPath=GetProperties.readValue("picpath")+"/"+filePath;
	System.out.println("rootPath===>>"+rootPath);
	if (checkfile(rootPath))
	{
		System.out.println("有图不用下了！");
		return rootPath;
	}


	try {
		save(is,rootPath);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return  filePath;
}

private static void save(InputStream is,String rootPath){
	BufferedInputStream in;
	FileOutputStream file;
	try
	{
		in = new BufferedInputStream(is);
		file = new FileOutputStream(new File(rootPath));
		int t;
		while ((t = in.read()) != -1) {
			file.write(t);
		}
		file.close();
		in.close();
	} catch (MalformedURLException e) {
//		logger.error("INFO: Get Picture Error MalformedURLException ["
//				+ e.getMessage() + "]");
		System.out.println("INFO: Get Picture Error MalformedURLException ["
				+ e.getMessage() + "]");
		throw new MyRunTimeExcption(e.getMessage());
	} catch (FileNotFoundException e) {
//		logger.error("INFO: Get Picture Error FileNotFoundException ["
//				+ e.getMessage() + "]");
		System.out.println("INFO: Get Picture Error FileNotFoundException ["
				+ e.getMessage() + "]");
		throw new MyRunTimeExcption(e.getMessage());
	} catch (IOException e) {
//		logger.error("INFO: Get Picture Error FileNotFoundException ["
//				+ e.getMessage() + "]");
		System.out.println("INFO: Get Picture Error FileNotFoundException ["
				+ e.getMessage() + "]");
		throw new MyRunTimeExcption(e.getMessage());
	}

}

	
private static boolean checkfile(String filepath)
{
	
	File oldFile = new File(filepath);
	if (oldFile.exists()) {
		return true;
	}
	return false;
	
}
	

}
